package cn.wit.contest.service.impl;

import cn.wit.contest.condition.ContestCondition;
import cn.wit.contest.mapper.ContestMapper;
import cn.wit.utils.pojo.Contest;
import cn.wit.contest.service.ContestService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ContestServiceImpl implements ContestService {
    @Autowired
    private ContestMapper contestMapper;

    @Override
    public Result insert(Contest contest) {
        int num = contestMapper.insert(contest);//插入一条数据
        if (num > 0) {
            return Result.ok();//表示插入成功
        }
        return Result.error("新增失败");
    }

    @Override
    public Result deleteById(int id) {
        int num = contestMapper.deleteById(id);
        if (num > 0) {
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @Override
    public Result updateById(Contest contest) {
        int num = contestMapper.updateById(contest);
        if (num > 0) {
            return Result.ok();//更新成功
        }
        return Result.error("更新失败");
    }

    @Override
    public Result selectById(Integer id) {
        return Result.ok(contestMapper.selectById(id));
    }

    @Override
    public Result queryContest(IPage<Contest> page, ContestCondition condition) {
        QueryWrapper<Contest> wrapper = new QueryWrapper<>();
        if (condition != null) {
            if (condition.getContestName() != null && condition.getContestName() != "") {
                wrapper.like("contest_name", condition.getContestName());
            }
            if (condition.getContestType() != null && condition.getContestType() != "") {
                wrapper.like("contest_type", condition.getContestType());
            }
            if (condition.getTimeType() != null && condition.getTimeType() != "") {
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
                switch (condition.getTimeType()) {
                    case "first"://查询全部的竞赛信息
                        break;
                    case "second"://即将发布
                        wrapper.gt("begin_time", sdf.format(date));
                        break;
                    case "third"://进行中
                        wrapper.le("begin_time", sdf.format(date));
                        wrapper.ge("end_time", sdf.format(date));
                        break;
                    case "fourth"://往期比赛
                        wrapper.lt("end_time", sdf.format(date));
                        break;
                }
            }
        }
        IPage<Contest> contestIPage = contestMapper.selectPage(page, wrapper);
        Result result = Result.ok(contestIPage);
        return result;
    }


    @Override
    public Result excelImport(MultipartFile file) {
        InputStream inputStream=null;
        int num = 0;
        try {
            inputStream=file.getInputStream();//将文件转换为输入流u
            Workbook workbook=WorkbookFactory.create(inputStream);//获得工作簿
            Sheet sheet=workbook.getSheetAt(0);//获得工作空间
            int numRows = sheet.getPhysicalNumberOfRows();
            for(int i=1;i<numRows;i++){//自动跳过标题头
                Row row = sheet.getRow(i);
                Contest contest=new Contest();
                String contest_type = row.getCell(0,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(row.getCell(0,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType()!= CellType.BLANK && contest_type!=null && contest_type!=""){
                    contest.setContestType(contest_type);
                }
                String contest_name = row.getCell(1,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(row.getCell(1,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType()!=CellType.BLANK&&contest_name!=null&&contest_name!=""){
                    contest.setContestName(contest_name);
                }
                if(row.getCell(2,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(row.getCell(2))){
                    // 转换日期/时间为字符串格式
                    Date date = row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String entry_begin_time = sdf.format(date);
                    contest.setEntryBeginTime(entry_begin_time);
                }
                if(row.getCell(3,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(row.getCell(3))){
                    // 转换日期/时间为字符串格式
                    Date date = row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String entry_end_time = sdf.format(date);
                    contest.setEntryEndTime(entry_end_time);
                }
                if(row.getCell(4,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(row.getCell(4))){
                    // 转换日期/时间为字符串格式
                    Date date = row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String begin_time = sdf.format(date);
                    contest.setBeginTime(begin_time);
                }
                if(row.getCell(5,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(row.getCell(5))){
                    // 转换日期/时间为字符串格式
                    Date date = row.getCell(5, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String end_time = sdf.format(date);
                    contest.setEndTime(end_time);
                }
                String contest_master = row.getCell(6, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(contest_master!=null&&contest_master!=""){
                    contest.setContestMaster(contest_master);
                }
                String contest_rank = row.getCell(7,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(contest_rank!=null&&contest_rank!=""){
                    contest.setContestRank(contest_rank);
                }
                String linking = row.getCell(8, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(linking!=null&&linking!=""){
                    contest.setLinking(linking);
                }
                String contest_command=row.getCell(9,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(contest_command!=null&&contest_command!=""){
                    contest.setContestCommand(contest_command);
                }
                String contest_desc=row.getCell(10, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(contest_desc!=null && contest_desc!=""){
                    contest.setContestDesc(contest_desc);
                }
                String contest_form = row.getCell(11,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(contest_form!=null && contest_form!=""){
                    contest.setContestForm(contest_form);
                }
                String enroll_material = row.getCell(12, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
                if(enroll_material!=null && enroll_material!=""){
                    contest.setEnrollMaterial(enroll_material);
                }
                int count=contestMapper.insert(contest);
                num+=count;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(num>0){
            return Result.ok();
        }else{
            return Result.error("导入excel出错");
        }
    }
}
